/*
 * TODO
 */
package com.xtrip.net.response;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.logging.impl.Log4JLogger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;

import com.xtrip.util.parse.xml.XtripDocumentHelper;

/**
 * @project xtrip
 * @author shiming.yao
 * @time 2013-5-31
 */
public abstract class TcBaseResponse {

	private String rspType;
	private String rspCode;
	private String rspDesc;
	
	private boolean isSucc = false;
	private boolean hasData = false;
	private Log4JLogger log = new Log4JLogger(TcBaseResponse.class.getName());
	private Element getRootElement(Document doc){
		return doc.getRootElement();
	}
	public void parse(String data){
		Document doc = null;
		try {
			doc = XtripDocumentHelper.read(data);
		} catch (UnsupportedEncodingException e) {
			log.debug("同程数据返回解析错误:编码不支持");
			e.printStackTrace();
		} catch (DocumentException e) {
			log.debug("同程数据返回解析错误：解析为Document出错");
			e.printStackTrace();
		}
		Element root = getRootElement(doc);
		Element header = root.element("header");
		Element body = root.element("body");
		parseHeader(header);
		parseBody(body);
	}
	protected void parseHeader(Element header){
		List<Node> list = header.elements();
		for(Node n : list){
			String name = n.getName();
			String value = n.getStringValue();
			System.out.println(name+":"+value);
			if(name.equals("rspType")){
				if(value.equals("0")){
					setSucc(true);
				}
				setRspType(value);
			} else if(name.equals("rspCode")){
				if(value.equals("0000")){
					setHasData(true);
				}
				setRspCode(value);
			}else if(name.equals("rspDesc")){
				setRspDesc(value);
			}
		}
		System.out.println("17u response : rspType = "+getRspType()+", rspCode = "+getRspCode()+", rspDesc = "+getRspDesc());
	}
	protected abstract void parseBody(Element body);
	public String getRspType() {
		return rspType;
	}
	public void setRspType(String rspType) {
		this.rspType = rspType;
	}
	public String getRspCode() {
		return rspCode;
	}
	public void setRspCode(String rspCode) {
		this.rspCode = rspCode;
	}
	public String getRspDesc() {
		return rspDesc;
	}
	public void setRspDesc(String rspDesc) {
		this.rspDesc = rspDesc;
	}
	public boolean isSucc() {
		return isSucc;
	}
	public void setSucc(boolean isSucc) {
		this.isSucc = isSucc;
	}
	public boolean isHasData() {
		return hasData;
	}
	public void setHasData(boolean hasData) {
		this.hasData = hasData;
	}
}
